


//  gcc ./cJSON/cJSON.c cjson_test.c -o 1  -lm
//  gcc cJSON.c cjson_test.c -o cjson_test  -lm


#include <bits/pthreadtypes.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "../utility/cJSON.h"

//#include "Node95.h"
#include "file_util.h"

#include "gw_ktj_tcp_client.h" 

#include "print_log.h"

#include "gui_cjson_api.h"
#include "gui_logist.h"


//gw_1C3283005078

//char cct_id_str[]="010000000780";
//const char gui_cct_id_str[]="gw_1C3283005078";


#if 0   //用定时器，不用线程
 pthread_t gui_logist_app_tid;
#endif


static char gui_logist_data_update_cmd =0;

gui_gw_param_st   gui_gw_param;
nv_gw_msg_u nv_gw_msg_obj;

   gui_gw_com_t  gui_gw_com;


//EthParam_st   EthParam;
EthString_st  EthString;


void gui_logist_init(void )
{

FILE *fp = NULL;

//char buf[1024];
   memset((char*) &gui_gw_com,0,sizeof(gui_gw_com_t) );
   memset((char*) &gui_gw_param,0,sizeof(gui_gw_param_st) );
   memset((char*) nv_gw_msg_obj.Buffer,0,sizeof(nv_gw_msg_u) );


   gui_logist_data_update_cmd =0;

    fp = fopen(nv_gw_msg_file_path,"r");  

    if(fp != NULL) {

        printf("open file %s ok \n", nv_gw_msg_file_path );

      // read_total_file(fp, &gui_gw_param.nv_gw_msg, sizeof(nv_gw_msg_t));
      // read_size_file(fp,0, &gui_gw_param.nv_gw_msg, sizeof(nv_gw_msg_t));

      //  read_size_file(fp,0, buf, sizeof(nv_gw_msg_t) ) ;

      // PrintHex( buf,sizeof(nv_gw_msg_t) );
    // PrintHex2String( uint8_t *buf,uint16_t len);


      // memcpy( nv_gw_msg_obj.Buffer , buf, sizeof(nv_gw_msg_t) );

    //  read_size_file(fp,0, nv_gw_msg_obj.Buffer, sizeof(nv_gw_msg_u) ) ;
    //   PrintHex( nv_gw_msg_obj.Buffer,sizeof(nv_gw_msg_u)  ); 

       read_size_file(fp,0, (uint8_t*)&gui_gw_param.nv_gw_msg, sizeof(nv_gw_msg_t) );

       
       PrintHex( (uint8_t*)&gui_gw_param.nv_gw_msg,sizeof(nv_gw_msg_t) ); 

       fclose(fp);

    }else{
        printf("open file %s fail \n", nv_gw_msg_file_path );

    }

    
#if  0
   printf("gatewayId=%s\n", nv_gw_msg_obj.Reg.gatewayId );
   printf("plcMac=%s\n", nv_gw_msg_obj.Reg.plcMac );
   printf("eth_mac=%s\n", nv_gw_msg_obj.Reg.eth_mac );
   printf("masterId=%s\n", nv_gw_msg_obj.Reg.masterId );


   printf("hwVer=%s\n", nv_gw_msg_obj.Reg.hwVer );
   printf("swVer=%s\n", nv_gw_msg_obj.Reg.swVer );
   printf("pid_gateway=%s\n", nv_gw_msg_obj.Reg.pid_gateway );


   printf("ct=%d\n", nv_gw_msg_obj.Reg.gw_param.ct );
   printf("vt=%d\n", nv_gw_msg_obj.Reg.gw_param.vt );
   printf("tz=%d\n", nv_gw_msg_obj.Reg.gw_param.tz );
   printf("lat=%d\n", nv_gw_msg_obj.Reg.gw_param.lat );
   printf("lng=%d\n", nv_gw_msg_obj.Reg.gw_param.lng );

#endif

#if  1

   printf("gatewayId=%s\n", gui_gw_param.nv_gw_msg.gatewayId );
   printf("plcMac=%s\n", gui_gw_param.nv_gw_msg.plcMac );
   printf("ethMac=%s\n", gui_gw_param.nv_gw_msg.ethMac );
   printf("masterId=%s\n", gui_gw_param.nv_gw_msg.masterId );


   printf("hwVer=%s\n", gui_gw_param.nv_gw_msg.hwVer );
   printf("swVer=%s\n", gui_gw_param.nv_gw_msg.swVer );
   printf("pid_gateway=%s\n", gui_gw_param.nv_gw_msg.pid_gateway );


   printf("ct=%d\n", gui_gw_param.nv_gw_msg.gw_param.ct );
   printf("vt=%d\n", gui_gw_param.nv_gw_msg.gw_param.vt );
   printf("tz=%d\n", gui_gw_param.nv_gw_msg.gw_param.tz );
   printf("lat=%d\n", gui_gw_param.nv_gw_msg.gw_param.lat );
   printf("lng=%d\n", gui_gw_param.nv_gw_msg.gw_param.lng );

#endif  

	//read_total_file(fp, &gui_gw_param.nv_gw_msg, sizeof(nv_gw_msg_t));

    //进程开始运行的时候读取一次数据
   gui_logist_setdataupdate( );    //刷新周期读取数据

}


gui_gw_param_st* gui_get_gw_param(void )
{
   return (gui_gw_param_st*) &gui_gw_param;
}


//------周期刷新数据 或者按键事件触发-------由按键触发-----------------
//------Menu4_2_1, Menu4_3_1, Menu4_4_1, -Menu4_5_1 --需要刷新读周期--

void gui_logist_setdataupdate(void )
{
    gui_logist_data_update_cmd =1;
}



void gui_logist_tick_sec(void )
{
static uint8_t step =0 ;
static uint8_t index =0 ;

   switch (step) {
     case 0:
     {

      //返回值 0---链接断开，  1---链接成功
         if( 0== gw_ktj_IsTcpClientConnected( ) ) 
         {
             break;
         }

         if( strcmp("",get_gateway_id()) )
         {//0表示相等，!0表示不等
            index =0 ;        
            step++;
         }else{

             //3.14  读网关信息  readGwMsg  
              json_readGwMsg(  );
         }


     }break;


     case 1:
     {
         if(index>=READ_CMD_TAB_MAX)
         {
            //Node_Clear( );  //每次在读取白名单之前，清空节点的状态-----当节点很多时---读取白名单的耗时比较久---还需优化这部分逻辑
            index = 0;
            step++;
         }

         printf("%s index=%d ,tx_cmd=%s\n", __FUNCTION__,index, read_cmd_tab[index].cmd);
         read_cmd_tab[index].func( read_cmd_tab[index].cmd, get_gateway_id() , gui_js_api_msg_id++); //  gui_cct_id_str
         index++;

     }break;


     case 2:
     {
         if( gui_logist_data_update_cmd )
         {
            gui_logist_data_update_cmd = 0;
            index = 0;
            step  = 0;
         }
     }break;


     default:
     {
         index = 0;
         step  = 0;        
     }break;

   
   }


}







char* get_gateway_id(void)
{
  return  gui_gw_param.nv_gw_msg.gatewayId;  //   cct_id_str ;
}

char* get_gateway_swVer(void)
{
  return  gui_gw_param.nv_gw_msg.swVer; 
}

//hwVer=1.0.0
//swVer=2.4.18

char* get_gateway_hwVer(void)
{
  return  gui_gw_param.nv_gw_msg.hwVer;  
}

uint8_t gui_get_4G_CSQ(void)
{
    return  gui_gw_param.csq;
}




char* get_plc_mac(void)
{
  return  gui_gw_param.nv_gw_msg.plcMac; 
}



char* get_eth_mac(void)
{
  return  gui_gw_param.nv_gw_msg.ethMac; 
}

char* get_ble_mac(void)
{
  return  gui_gw_param.nv_gw_msg.bleMac; 
}



